#include "userload.h"
#include "maindialog.h"

#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
#include <QMessageBox>


UserLoad::UserLoad(QWidget *parent)
    : QDialog(parent)
{
    resize(500, 400);

    QPushButton* btnLogin = new QPushButton(this);
    btnLogin->setText("登 录");
    btnLogin->move(230, 200);
    btnLogin->show();


    QLabel* labelUserName = new QLabel(this);
    labelUserName->setText("用户名：");
    labelUserName->move(100, 100);

    edtUserName = new QLineEdit(this);
    edtUserName->move(100 + labelUserName->width(), 100);
    edtUserName->setFocus();

    QLabel* labelUserPass = new QLabel(this);
    labelUserPass->setText("密码：");
    labelUserPass->move(100, 150);

    edtUserPass = new QLineEdit(this);
    edtUserPass->move(100 + labelUserPass->width(), 150);
    edtUserPass->setEchoMode(QLineEdit::Password);

    btnLogin->move(edtUserName->x(), 200);

    connect(btnLogin, &QPushButton::clicked, this, &UserLoad::loginCheck);
}

UserLoad::~UserLoad()
{
}

void UserLoad::loginCheck()
{
    qDebug() << "login!";

    QString userName, userPass;

    userName = edtUserName->text();
    userPass = edtUserPass->text();

    if(userName == "yjx" && userPass == "yjx666" ||
            userName == "dongge" && userPass == "123456")
    {

        // 弹出非模态对话框
        MainDialog* dlg = new MainDialog(userName, this);

        dlg->setAttribute(Qt::WA_DeleteOnClose);

        dlg->show();
        accept();
    }
    else
    {
        // 登录失败
        QMessageBox::critical(this, "提示", "您的账号或密码错误，登录失败！");

    }
}

